package cn.aws360.boot.system.starter.controller;

import cn.aws360.boot.base.api.annotation.OpsLog;
import cn.aws360.boot.base.api.constant.enums.OpsTypeEnum;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import cn.aws360.boot.base.api.utils.ServletUtils;
import cn.aws360.boot.base.api.validation.ValidationGroup;
import cn.aws360.boot.data.api.pojo.request.PageQuery;
import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.aws360.boot.system.api.pojo.request.SysFileQuery;
import cn.aws360.boot.system.api.pojo.request.SysFileWrite;
import cn.aws360.boot.system.api.pojo.response.SysFileModel;
import cn.aws360.boot.system.biz.service.SysFileService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static cn.aws360.boot.system.api.constant.SysConstant.*;

/**
 * @author junnan
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/file")
@ApiSupport(order = MODULE_FILE_CODE)
@Api(value = MODULE_FILE_NAME, tags = MODULE_FILE_TAGS)
public class SysFileController {

    private final SysFileService fileService;

    @GetMapping("/index.json")
    @SaCheckPermission("sys:file:query")
    @ApiOperation(value = "文件页面", hidden = true)
    public ResultModel<JSONObject> index() {
        return ResultModel.ok(ServletUtils.getPageJson());
    }

    @GetMapping("/list")
    @SaCheckPermission("sys:file:query")
    @ApiOperation(value = "文件列表")
    @ApiOperationSupport(order = 1)
    public ResultModel<List<SysFileModel>> list(SysFileQuery SysFileQuery) {
        return ResultModel.ok(fileService.findList(SysFileQuery));
    }

    @GetMapping("/page")
    @SaCheckPermission("sys:file:query")
    @ApiOperation(value = "文件分页")
    @ApiOperationSupport(order = 1)
    public ResultModel<PageResult<SysFileModel>> page(SysFileQuery sysFileQuery, PageQuery pageQuery) {
        return ResultModel.ok(fileService.findPage(sysFileQuery, pageQuery));
    }

    @GetMapping("/{id}")
    @SaCheckPermission("sys:file:detail")
    @ApiOperation(value = "文件详情")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "id", value = "文件 id", dataTypeClass = Integer.class, required = true)
    public ResultModel<SysFileModel> detail(@PathVariable Integer id) {
        return ResultModel.ok(fileService.detail(id));
    }

    @PostMapping
    @SaCheckPermission("sys:file:add")
    @OpsLog(title = "新增文件", opsType = OpsTypeEnum.INSERT)
    @ApiOperation(value = "新增文件")
    @ApiOperationSupport(order = 2)
    public ResultModel<Boolean> add(@Validated(ValidationGroup.add.class) @RequestBody SysFileWrite sysFileWrite) {
        return ResultModel.ok(fileService.add(sysFileWrite));
    }

    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:file:remove")
    @OpsLog(title = "移除文件", opsType = OpsTypeEnum.REMOVE)
    @ApiOperation(value = "移除文件")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParam(name = "ids", value = "文件 ids", dataTypeClass = String.class, required = true)
    public ResultModel<Boolean> remove(@PathVariable String ids) {
        return ResultModel.ok(fileService.remove(ids));
    }

    @PostMapping("/upload/{bucket}/{directory}")
    @OpsLog(title = "上传文件", opsType = OpsTypeEnum.INSERT)
    @ApiOperation(value = "上传文件")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucket", value = "容器名称", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "directory", value = "文件目录", dataTypeClass = String.class, required = true)
    })
    public ResultModel<SysFileModel> upload(@PathVariable String bucket, @PathVariable String directory,
                                            @RequestPart("file") MultipartFile multipartFile) {
        return ResultModel.ok(fileService.upload(bucket, directory, multipartFile));
    }

    @GetMapping("/download/{id}")
    @ApiOperation(value = "下载文件")
    @ApiOperationSupport(order = 12)
    public void download(@PathVariable Integer id) {
        fileService.download(id);
    }

}
